package personal.xjl._23java1studentdemo.controller;

import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import personal.xjl._23java1studentdemo.mapper.StudentMapper;
import personal.xjl._23java1studentdemo.model.Student;
import personal.xjl._23java1studentdemo.service.StudentService;

import java.util.ArrayList;
import java.util.List;

//相当于servlet
@Controller
@RequestMapping("/Student")
public class StudentController {
    @Autowired
    StudentService studentService;

    //查询
    @RequestMapping("list")
    public String list(Model model) {
        List<Student> students = studentService.findAll();
        model.addAttribute("students", students);
        return "ListStudents";
    }

    @RequestMapping("add")
    public String addStudent(Student student){
      studentService.add(student);
      return "redirect:/Student/list/1/3";
    }
      @RequestMapping("toAdd")
    public String toAdd(Model model) {
        List<Student> Students = StudentService.findAll();
        model.addAttribute("Students", Students);
        return "addStudent";
    }

    @RequestMapping("remove")
    public String remove(int id){
        studentService.remove(id);
        return "redirect:/Student/list/1/3";
    }
   @RequestMapping("toPage")
   public String toPage(String page){
        return page;
   }

   @RequestMapping("edit")
    public String edit(Student student){
        studentService.edit(student);
        return "redirect:/Student/list/1/3";
   }
   @RequestMapping("toEdit")
    public String toEdit(int id, Model model){
        Student student = studentService.findById(id);
        model.addAttribute("student", student);
        return "editStudent";
   }

   @RequestMapping("list/{currPage}/{pageSize}")
    public String list(Model model, @PathVariable("currPage") int currPage, @PathVariable("pageSize") int pageSize){
       if(currPage < 1) currPage = 1;
       if(pageSize < 1) pageSize = 1;
    List<Student> students = studentService.findByPage(currPage, pageSize);
    model.addAttribute("students", students);
    return "ListStudents";
   }

   @RequestMapping("loginValidate")
    public String login(String username, String password, Model model,HttpSession session){
       if ( studentService.login(username,password)){

           session.setAttribute("username", username);
           return "redirect:/Student/list/1/3";
       }else{
           return "redirect:/Student/toPage?page=login";
       }
   }
   @RequestMapping("login")
    public String login(){
        return "login";
   }

   @RequestMapping("logout")
    public String logout(HttpSession session){
        session.invalidate();//将session取消掉，让令牌无效
       return "login";
   }
}
